package cn.nfwx.tv.fragment.nyqf;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chan.net.HttpCallback;
import com.chan.net.Result;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.nfwx.tv.R;
import cn.nfwx.tv.activity.HomeActivity;
import cn.nfwx.tv.adapter.NyqfLiveAdapter;
import cn.nfwx.tv.base.BaseFragment;
import cn.nfwx.tv.helper.DialogFragmentHelper;
import cn.nfwx.tv.manager.NyqfManager;
import cn.nfwx.tv.model.TvChannel;
import cn.nfwx.tv.util.RecyclerViewUtil;
import cn.nfwx.tv.util.SharePrefUtil;

public class LiveFragment extends BaseFragment {

    @BindView(R.id.rcv_main)
    RecyclerView rcvMain;

    @BindView(R.id.fl_content)
    FrameLayout flContent;

    private Unbinder unbinder;

    NyqfLiveAdapter mAdapter;

    public static LiveFragment newInstance() {
        LiveFragment fragment = new LiveFragment();
        return fragment;
    }

    @Override
    public View initView() {
        return View.inflate(mContext, R.layout.fragment_cpp, null);
    }

    @Override
    protected boolean isLoadView() {
        return true;
    }

    @Override
    protected void bindView() {
        RecyclerViewUtil.setLinearLayoutManagerWithDivder(mContext, rcvMain);
        mAdapter = new NyqfLiveAdapter();
        rcvMain.setAdapter(mAdapter);
        RecyclerView.ItemDecoration itemDecoration =
                new HorizontalDividerItemDecoration.Builder(mContext)
                        .size(2)
                        .color(Color.parseColor("#99b2b2b2"))
                        .marginResId(R.dimen.dp15, R.dimen.dp15)
                        .build();
        rcvMain.addItemDecoration(itemDecoration);
    }

    @Override
    protected void initLister() {
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(final BaseQuickAdapter adapter, View view, final int position) {
                DialogFragmentHelper.showConfirmDialog(getChildFragmentManager(), null, "切换到“南方无限电视”观看电视直播?", "切换", "取消", true, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TvChannel tvChannel = (TvChannel) adapter.getItem(position);
                        SharePrefUtil.saveIjkLiveId(mContext, tvChannel.getGid());
                        Intent intent = new Intent(mContext, HomeActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        getActivity().finish();
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogFragmentHelper.closeConfirmDialog(getChildFragmentManager());
                    }
                });
            }
        });
    }

    @Override
    public void initData() {
        loadView.startLoading(flContent);
        NyqfManager.getChannelLives(this.<Result<List<TvChannel>>>bindToLifecycle(), new HttpCallback<List<TvChannel>>() {
            @Override
            public void onSuccess(String requestCommand, Result<List<TvChannel>> data) {
                loadView.removeParentView(flContent);
                mAdapter.setNewData(data.getData());
            }

            @Override
            public void onFail(String requestCommand, Result result) {
                loadView.removeParentView(flContent);
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
